package com.weasing.res.web.action;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.weasing.res.domain.WxDict;
import com.weasing.res.service.IWxDictService;
import com.weasing.res.utils.DicUtils;
import com.weasing.res.utils.RESUtils;
import com.weasing.res.utils.StringUtil;
import com.weasing.res.web.action.base.BaseAction;

import net.sf.json.JSONArray;

@Controller
@Scope("prototype")
public class WxDictAction extends BaseAction<WxDict> {
	@Autowired
	private IWxDictService wxDictService;

	private List<WxDict> wxDictList;

	private int currentDicId;
	
	public void findWxDictByDictName() {
		String dictinfo = wxDictService.findWxDictByDictName(model
				.getDictname());
		List<String> dictinfos = StringUtil.String2List(dictinfo);
		String json = JSONArray.fromObject(dictinfos).toString();
		ServletActionContext.getResponse().setContentType(
				"text/json;charset=utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 进入页面的方法
	 * 
	 * @return
	 */
	public String findAll() {
		
		if (null == wxDictList) {
			wxDictList = wxDictService.findAll();
		}

		WxDict wxDict = new WxDict();
		if (currentDicId == 0)
			currentDicId = 1;

		wxDict = wxDictList.get(currentDicId-1);
		String dictinfo = wxDict.getDictinfo();
		List<String> infoList = DicUtils.string2List(dictinfo);
		// 字典集合 select
		RESUtils.getRequest().setAttribute("wxDictList", wxDictList);
		// 当前字典
		RESUtils.getRequest().setAttribute("wxDict", wxDict);
		// 当前字典的内容
		RESUtils.getRequest().setAttribute("infoList", infoList);

		return "list";
	}

	public void addDic() {
		if (currentDicId == 0)
			currentDicId = 1;


	}

	public int getCurrentDicId() {
		return currentDicId;
	}

	public void setCurrentDicId(int currentDicId) {
		this.currentDicId = currentDicId;
	}

}
